<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/14
 * Time: 14:43
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class TechnicalGroupModel extends Model
{
    const TABLE = 'technical_group';
    private $startTime = '';
    private $endTime = '';

    public function __construct()
    {
        $yearInfo = (new YearModel())->findYear(date('Y-m-d H:i:s'));
        if (!empty($yearInfo)){
            $this->startTime = $yearInfo['startTime'];
            $this->endTime = $yearInfo['endTime'];
        }else{
            $this->startTime = date('Y-01-01 00:00:00');
            $this->endTime = date('Y-12-31 23:59:59');
        }
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        $result = DB::table(self::TABLE)
            ->leftJoin('profession as p','p.id','=',self::TABLE.'.professionId')
            ->leftJoin('employee','employee.id','=',self::TABLE.'.employeeId')
            ->leftJoin('group','group.id','=',self::TABLE.'.groupId')
            ->where(function ($query) use ($data) {
                $query->where('companyId',$data['companyId']);
                if (!empty($data['professionId'])) {
                    $query->where(self::TABLE . '.professionId', '=', $data['professionId']);
                }
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE . '.chargePerson', 'like', '%' . $data['search'] . '%');
                }
                if (!empty($this->startTime) && !empty($this->endTime)) {
                    $query->whereBetween(self::TABLE . '.createTime', [$this->startTime, $this->endTime]);
                }
            });
        if (!empty($param['length']) && !empty($param['page'])){
            $limit = config('yucheng.limit');
            $start = 0;
            if (!empty($param['length'])) {
                $limit = $param['length'];
            }
            if (!empty($param['page'])) {
                $start = ($param['page']-1)*$limit;
            }
            $result = $result->offset($start)->limit($limit);
        }
        return $result->orderBy('order', 'desc')
            ->select(self::TABLE . '.*','p.name as professionName','employee.jobNumber','group.name as groupName')
            ->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                $query->where('companyId',$data['companyId']);
                if (isset($data['professionId']) && !is_null($data['professionId'])) {
                    $query->where(self::TABLE . '.professionId', '=', $data['professionId']);
                }
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE . '.chargePerson', 'like', '%' . $data['search'] . '%');
                }
            })
            ->count();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function insert(array $param)
    {
        return DB::table(self::TABLE)->insertGetId($param);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    /**
     * @param array $array
     * @param array $data
     * @return mixed
     */
    public function update(array $array, array $data)
    {
        return DB::table(self::TABLE)->where($array)->update($data);
    }
}